package com.example.demo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

/**
 * Created by sunmo on 2016/12/5.
 */
public class FluentViewPager extends ViewPager {
//    private OnPageSelectedListener onPageSelectedListener;
    public FluentViewPager(Context context) {
        super(context);
    }

    public FluentViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        return updateChildDrawingOrder(childCount, i);
//        return super.getChildDrawingOrder(childCount, i);
    }

    //设置子视图的顺序
    private int updateChildDrawingOrder(int childCount, int i) {
        /**
         * schema-1:
         */
//        int selectedIndex = selectedPosition - 0;
//         Just to be safe
//        if (selectedIndex < 0) return i;
//
//        if (i == childCount - 1) {
//            // Draw the selected child last
//            return selectedIndex;
//        } else if (i >= selectedIndex) {
//            // Move the children after the selected child earlier one
//            return i + 1;
//        } else {
//            // Keep the children before the selected child the same
//            return i;
//        }
        /**
         * schema-3:
         */
        int selectedIndex = getCurrentItem();

        if (selectedIndex < 0) {
            return i;
        }

        if (i < selectedIndex) {
            return i;
        } else if (i >= selectedIndex) {
            return childCount - 1 - i + selectedIndex;
        } else {
            return i;
        }
    }

//    //set on page selected listener
//    public void setOnPageSelectedListener(OnPageSelectedListener onPageSelectedListener) {
//        this.onPageSelectedListener = onPageSelectedListener;
//    }
//
//    public interface OnPageSelectedListener {
//        void OnPageSelected(int postion);
//    }
}
